3D Graphics Programming with QuickDraw 3D 1.5.4
Previous | QD3D Book | Overview | Chapter Contents | Next |
Usually, you'll want to display the two-dimensional image of a three-dimensional model in a window. To do this, it's useful to define a custom window information structure that holds all the information about the QuickDraw 3D objects that are associated with the window. In the simplest cases, this information includes the model itself, the view, the illumination shading to be applied, and the desired styles of rendering the model. You might define a window information structure like this:
struct WindowInfo {
TQ3ViewObject view;
TQ3GroupObject model;
TQ3ShaderObject illumination;
TQ3StyleObject interpolation;
TQ3StyleObject backfacing;
TQ3StyleObject fillstyle;
};
typedef struct WindowInfo WindowInfo, *WindowInfoPtr, **WindowInfoHandle;
A standard way to attach an application-defined data structure (such as the WindowInfo structure) to a window is to set a handle to that structure as the window's reference constant. This technique is used in Listing 5 .
For a more complete description of using a window's reference constant to maintain window-specific information, see the discussion of document records in Inside Macintosh: Overview .
Listing 5 Creating a new window and attaching a window information structure
void MyNewWindow (void)
{
WindowPtr myWindow;
Rect myBounds = {42, 4, 442, 604};
WindowInfoHandle myWinfo;
/*Create new window.*/
myWindow = NewCWindow(0L, &myBounds, "\pWindow!", 1, documentProc,
(WindowPtr) -1, true, 0L);
if (myWindow == NULL)
goto bail;
SetPort(myWindow);
/*Create storage for the new window and attach it to window.*/
myWinfo = (WindowInfoHandle) NewHandle(sizeof(WindowInfo));
if (myWinfo == NULL)
goto bail;
SetWRefCon(myWindow, (long) myWinfo);
HLock((Handle) myWinfo);
/*Create a new view.*/
(**myWinfo).view = MyNewView(myWindow);
if ((**myWinfo).view == NULL)
goto bail;
/*Create model to display.*/
(**myWinfo).model = MyNewModel(); /*see Listing 0-4*/
if ((**myWinfo).model == NULL)
goto bail;
/*Configure an illumination shader.*/
(**myWinfo).illumination = Q3PhongIllumination_New();
if ((**myWinfo).illumination == NULL)
goto bail;
/*Configure the rendering styles.*/
(**myWinfo).interpolation =
Q3InterpolationStyle_New(kQ3InterpolationStyleNone);
if ((**myWinfo).interpolation == NULL)
goto bail;
(**myWinfo).backfacing =
Q3BackfacingStyle_New(kQ3BackfacingStyleRemoveBackfacing);
if ((**myWinfo).backfacing == NULL)
goto bail;
(**myWinfo).fillstyle = Q3FillStyle_New(kQ3FillStyleFilled);
if ((**myWinfo).fillstyle == NULL)
goto bail;
HUnlock((Handle) myWinfo);
return;
bail:
/*If failed for any reason, then close the window.*/
if (myWinfo != NULL)
DisposeHandle((Handle) myWinfo);
if (myWindow != NULL)
DisposeWindow(myWindow);
}
The MyNewWindow function creates a new window and a new window information structure, attaches the structure to the window, and then fills out several fields of that structure. In particular, MyNewWindow creates a new illumination shader that implements a Phong illumination model. You need an illumination shader for a view's lights to have any effect. (See the chapter "Shader Objects" for complete information on the available illumination shaders.) Then MyNewWindow disables interpolation between vertices of faces, removes unseen backfaces of objects in the model, and sets the renderer to render filled faces on those objects. These settings are actually passed to the renderer by submitting the styles during rendering. See "Rendering a Model" for details.
The MyNewWindow function can leak memory. Your application should use a different error-recovery strategy than is used in Listing 5 .
Previous | QD3D Book | Overview | Chapter Contents | Next |